package com.showtao.front.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.showtao.common.controller.BaseController;
import com.showtao.front.domain.Article;
import com.showtao.front.domain.Catalog;
import com.showtao.front.domain.Product;
import com.showtao.front.domain.ProductCategory;
import com.showtao.front.domain.VehicleModel;
import com.showtao.front.dto.CatalogDto;
import com.showtao.front.dto.LabelItemDto;
import com.showtao.front.service.ContentService;
import com.showtao.front.service.ProductService;
import com.showtao.front.service.VehicleService;

@Controller
public class IndexController extends BaseController {

	@Autowired
	private transient ProductService productService;
	@Autowired
	private transient VehicleService vehicleService;
	@Autowired
	private transient ContentService contentService;

	@ModelAttribute("navCatalogs")
	public List<CatalogDto> findNavCatalogs() {
		List<CatalogDto> result = new ArrayList<CatalogDto>();
		List<Catalog> cts = contentService.findNavCatalogs();
		for (Catalog ct : cts) {
			result.add(CatalogDto.from(ct));
		}
		return result;
	}

	@RequestMapping(value = "/")
	public String home(Model model) throws Exception {
		model.addAttribute("menuItem", "home");
		List<ProductCategory> categories = productService.findAllProductCategories();
		List<VehicleModel> hotVms = vehicleService.getTop5VehicleModel();
		model.addAttribute("categories",categories);
		model.addAttribute("hotVms", hotVms);
		if(categories.size() !=0 ){
		    model.addAttribute("selectedCategory", categories.get(0).getCode());
		}
		return "home";
	}
	
	@RequestMapping(value = "/selection")
	public String selection(
			@RequestParam(value = "category", required = false, defaultValue = "cover") String category,
			Model model) throws Exception {
		model.addAttribute("menuItem", "selection");
		model.addAttribute("categories",
				productService.findAllProductCategories());
		model.addAttribute("category", category.toLowerCase());
		model.addAttribute("productCategory",
				productService.getProductCategoryByCode(category));
		String tag = category.toLowerCase() + "_summary";
		Article article = contentService.getTheFirstArticleByTagAlias(tag);
		if (article != null) {
			model.addAttribute("articleExist", true);
			model.addAttribute("article", article);
		}
		return "product/selection";
	}

	@RequestMapping(value = "/selection/{modelId}")
	public String selection(
			@PathVariable("modelId") Long modelId,
			@RequestParam(value = "category", required = false, defaultValue = "cover") String category,
			Model model)
			throws Exception {
	    vehicleService.increaseViewCount(modelId);
		model.addAttribute("menuItem", "selection");
		model.addAttribute("categories",
				productService.findAllProductCategories());
		model.addAttribute("category", category.toLowerCase());
		model.addAttribute("modelId", modelId);
		return "product/selection";
	}
	
	@RequestMapping(value = "/order")
    public String toOrderQuery(Model model){
        model.addAttribute("menuItem", "order");
        return "order/list";
    }
	
	@RequestMapping(value = "/myorder")
    public String toOrderMyQuery(Model model){
        model.addAttribute("menuItem", "myorder");
        return "myorder/myorderlist";
    }

	@RequestMapping(value = "/selection/{modelId}/products/{productId}")
	@RequiresPermissions (value={"product_detail_view"})
	public String selectProduct(
			@PathVariable("modelId") Long modelId,
			@PathVariable("productId") Long productId, 
			Model model) throws Exception {
		model.addAttribute("menuItem", "selection");
		model.addAttribute("categories",
				productService.findAllProductCategories());
		Product product = productService.getOneJoinFully(productId);
		List<LabelItemDto> labels = vehicleService
				.getVehicleBreadCrumb(modelId);
		LabelItemDto productLabel = new LabelItemDto();
		productLabel.setId(product.getId());
		productLabel.setLabel(product.getCode());
		labels.add(productLabel);
		model.addAttribute("labels", labels);
		model.addAttribute("product", product);
		model.addAttribute("category", product.getCategory().getCode());
		model.addAttribute("modelId", modelId);
		return "product/single";
	}

	@RequestMapping(value = "/search")
	public String doSearch(
			@RequestParam(value = "q", required = false, defaultValue = "") String keywords,
			@RequestParam(value = "category", required = false, defaultValue = "cover") String category,
			Model model) {
		ProductCategory pc = productService.getProductCategoryByCode(category);
		if (pc != null) {
			model.addAttribute("categoryName", pc.getName());
		} else {
			model.addAttribute("categoryName", category);
		}
		List<VehicleModel> vms = vehicleService
				.findLimitedVehicleModelAccordingTo(keywords, 10);
		List<Product> products = productService.findLimitedProductAccordingTo(
				keywords, category, 10);
		model.addAttribute("vms", vms);
		model.addAttribute("products", products);
		model.addAttribute("resultCount", vms.size() + products.size());
		model.addAttribute("keywords", keywords);
		return "search_result";
	}

}
